﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.RoleManage
{
    public partial class RoleStatus : Common.PageBase
    {

        //private UserLoginInfo _userLoginInfo;
        private T_ROLE_BLL roleBll = new T_ROLE_BLL();
        private T_ROLEORDERSTATUSAUTHORITY_BLL roleOrderStatusAuthorityBll = new T_ROLEORDERSTATUSAUTHORITY_BLL();
        private T_WORKORDERTYPE_BLL workOrderTypeBll = new T_WORKORDERTYPE_BLL();
        private T_WORKORDERSTATUS_BLL workOrderStatusBll = new T_WORKORDERSTATUS_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            PageHelper.SetPrePageUrl(this.Page);

        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                ddlROLEIDBinding();

                tvWorkOrderStatusBinding();
            }
        }

        #region 页面初始化数据绑定



        private void ddlROLEIDBinding()
        {
            StringBuilder sql = new StringBuilder();
            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
            }
            ddlROLEID.DataSource = roleBll.GetList(sql.ToString());
            ddlROLEID.DataTextField = "RoleName";
            ddlROLEID.DataValueField = "RoleID";
            ddlROLEID.DataBind();
        }







        #endregion


        protected void ddlROLEID_SelectedIndexChanged(object sender, EventArgs e)
        {
            tvWorkOrderStatusBinding();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            List<T_ROLEORDERSTATUSAUTHORITY_Entity> list = new List<T_ROLEORDERSTATUSAUTHORITY_Entity>();
            getSelected(tvWorkOrderStatus.Nodes, ref list);

            try
            {
                roleOrderStatusAuthorityBll.Delete("ROLEID", ddlROLEID.SelectedValue.Trim());
                foreach (T_ROLEORDERSTATUSAUTHORITY_Entity en in list)
                {
                    roleOrderStatusAuthorityBll.Insert(en);
                }
                PageHelper.ShowMessage("权限设置成功!");
            }
            catch
            {
                PageHelper.ShowMessage("权限设置失败!");
            }
        }



        private void tvWorkOrderStatusBinding()
        {
            tvWorkOrderStatus.Nodes.Clear();
            List<T_WORKORDERTYPE_Entity> workOrderTypeList = workOrderTypeBll.GetCache_T_WORKORDERTYPE_List(this.Page);
            List<T_WORKORDERSTATUS_Entity> workOrderStatusList =workOrderStatusBll.GetPageItem_T_WORKORDERSTATUS_List(this.Page);
            List<T_ROLEORDERSTATUSAUTHORITY_Entity> roleorderstatusauthorityList =
                roleOrderStatusAuthorityBll.GetList(String.Format(" and ROLEID = '{0}'", ddlROLEID.SelectedValue.Trim()));
                 
            TreeNode pNode = new TreeNode();
            pNode.Text = "工单状态";
            pNode.ToolTip = "-1";

            foreach (var type in workOrderTypeList)
            {
                TreeNode node = new TreeNode(type.ORDERTYPENAME, type.ORDERTYPEID.ToString());
                node.ToolTip = "-1";
                WorkOrderStatusRecursiveBinding(workOrderStatusList, roleorderstatusauthorityList, node, type.ORDERTYPEID.ToString());
                pNode.ChildNodes.Add(node);
            }
            tvWorkOrderStatus.Nodes.Add(pNode);
            tvWorkOrderStatus.ExpandAll();
        }

        private void WorkOrderStatusRecursiveBinding(List<T_WORKORDERSTATUS_Entity> workOrderStatusList, List<T_ROLEORDERSTATUSAUTHORITY_Entity> roleorderstatusauthorityList, TreeNode parentNode, string orderTypeId)
        {
            List<T_WORKORDERSTATUS_Entity> subList = workOrderStatusList.FindAll(delegate(T_WORKORDERSTATUS_Entity en)
                                                                                     {
                                                                                         if (
                                                                                             en.ORDERTYPEID.ToString() ==
                                                                                             orderTypeId)
                                                                                             return true;
                                                                                         else
                                                                                             return false;
                                                                                     });
            foreach (var status in subList)
            {
                TreeNode node = new TreeNode(status.ORDERSTATUSNAME, status.ORDERSTATUSID.ToString());
                node.ToolTip = status.ORDERTYPEID.ToString();
                node.Checked = roleorderstatusauthorityList.Exists(delegate(T_ROLEORDERSTATUSAUTHORITY_Entity en)
                                                                       {
                                                                           if (en.ORDERSTATUSID.ToString() ==
                                                                               status.ORDERSTATUSID.ToString())
                                                                               return true;
                                                                           else
                                                                               return false;
                                                                       });
                parentNode.ChildNodes.Add(node);
            }
            
        }

 

       

        /// <summary>
        /// Gets the selected.
        /// </summary>
        /// <param name="nodes">The nodes.</param>
        /// <param name="list">The list.</param>
        private void getSelected(TreeNodeCollection nodes, ref List<T_ROLEORDERSTATUSAUTHORITY_Entity> list)
        {
            foreach (TreeNode node in nodes)
            {
                if (node.Checked && (node.ToolTip.Trim() != "-1"))
                {
                    T_ROLEORDERSTATUSAUTHORITY_Entity entity = new T_ROLEORDERSTATUSAUTHORITY_Entity();
                    entity.ROLEID = StringHelper.ToLong(ddlROLEID.SelectedValue.Trim(), 0);
                    entity.ROLENAME = ddlROLEID.SelectedItem.Text;
                    entity.ORDERSTATUSID = StringHelper.ToInt(node.Value, 0);
                    entity.ORDERSTATUSNAME = node.Text;
                    entity.ORDERTYPEID = StringHelper.ToInt(node.Parent.Value, 0);
                    entity.ORDERTYPENAME = node.Parent.Text;
                    entity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                    entity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                    entity.CREATETIME = DateTime.Now;
                    entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                    entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                    entity.LASTMODIFYTIME = DateTime.Now;
                    entity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                    entity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                    list.Add(entity);
                }
                getSelected(node.ChildNodes, ref list);
            }
        }

       
    }
}
